<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "tally_pm".
 *
 * @property int $id 账本管理员ID
 * @property int $tally_id 账本ID
 * @property int $uid 创建者
 * @property string $role 角色admin,finance
 * @property string $created_time 创建时间
 * @property string $updated_time 更新时间
 */
class TallyPm extends \yii\db\ActiveRecord
{

    /**
     * 管理员身份角色
     */
    const ROLE_ADMIN = 'admin'; // 超级管理员
    const ROLE_FINANCE = 'finance'; // 财务
    const ROLE_CASHIER = 'cashier'; // 出纳 具有审核权限
    const ROLE_ACCOUNTANT = 'accountant'; // 会计
    const ROLE_MANAGER = 'manager'; // 普通管理

    const ROLE_PROJECT_MANAGER = 'pmanger'; // 普通管理
    const ROLE_VIEWER = 'viewer'; // 观察者、老板、股东

    const STATE_VALID = 1; //状态正常
    const STATE_INVALID = 0;//状态失效

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'tally_pm';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['tally_id', 'uid','is_default'], 'integer'],
            [['role'], 'required'],
            [['created_time', 'updated_time','state'], 'safe'],
            [['role'], 'string', 'max' => 30],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'tally_id' => 'Tally ID',
            'uid' => 'Uid',
            'is_default' => 'Is Default',//  是否为默认账本
            'role' => 'Role',
            'state' => 'State',
            'created_time' => 'Created Time',
            'updated_time' => 'Updated Time',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTally()
    {
        return $this->hasOne(Tally::className(),['id' => 'tally_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(User::className(),['id' => 'uid']);
    }

    /**
     * @param $role
     * @desc 根据角色标识获取角色名称
     * @return string
     */
    public static function getRoleName($role)
    {
        $roleName = '';
        switch ($role){
            case self::ROLE_ADMIN:
                $roleName = '创建者';
                break;
            case self::ROLE_MANAGER:
                $roleName = '管理员';
                break;
            case self::ROLE_PROJECT_MANAGER:
                $roleName = '项目经理';
                break;
            case self::ROLE_FINANCE:
                $roleName = '财务';
                break;
            case self::ROLE_ACCOUNTANT:
                $roleName = '会计';
                break;
            case self::ROLE_CASHIER:
                $roleName = '出纳';
                break;
            case self::ROLE_VIEWER:
                $roleName = '老板/股东';
                break;
        }
        return $roleName;
    }
}
